package com.heatingcharge.service.charge.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.exception.BaseException;
import com.heatingcharge.common.util.ResponseCodeEnum;
import com.heatingcharge.dto.ChargePlanDTO;
import com.heatingcharge.entity.base.HeatYear;
import com.heatingcharge.entity.charge.ChargePlan;
import com.heatingcharge.entity.unit.ResZone;
import com.heatingcharge.mapper.charge.ChargePlanMapper;
import com.heatingcharge.param.ChargePlanParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IHeatYearService;
import com.heatingcharge.service.charge.IChargePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 收费计划 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-12-27
 */
@Service
public class ChargePlanServiceImpl extends ServiceImpl<ChargePlanMapper, ChargePlan> implements IChargePlanService {

    @Autowired
    IHeatYearService heatYearService;
    @Override
    public IPage<ChargePlanDTO> getChargePlans(Page page, ChargePlanParam chargePlanParam) {
        return baseMapper.selectPlantChargePlan(page,chargePlanParam);
    }

    @Override
    public boolean save(ChargePlanParam chargePlanParam) {
        if (checkExist(chargePlanParam.getHeatYearId(),null)){
            throw new BaseException(ResponseCodeEnum.CHARGEPLAN_EXIST.getMessage(),ResponseCodeEnum.CHARGEPLAN_EXIST.getCode());
        }
        HeatYear heatYear = heatYearService.getById(chargePlanParam.getHeatYearId());
        JwtUser user = SecurityUtil.getUser();
        ChargePlan chargePlan=ChargePlan.builder()
                .heatYearId(chargePlanParam.getHeatYearId())
                .planName(heatYear.getYearName()+"收费计划")
                .makingTime(new Date())
                .stopDate(chargePlanParam.getStopDate())
                .minPercent(chargePlanParam.getMinPercent())
                .maker(user.getRealName())
                .build();
        return baseMapper.insert(chargePlan)>0;
    }

    @Override
    public boolean update(ChargePlanParam chargePlanParam) {
        if (checkExist(chargePlanParam.getHeatYearId(),chargePlanParam.getId())){
            throw new BaseException(ResponseCodeEnum.CHARGEPLAN_EXIST.getMessage(),ResponseCodeEnum.CHARGEPLAN_EXIST.getCode());
        }
        ChargePlan oldObj = baseMapper.selectById(chargePlanParam.getId());
        if (ObjectUtil.isNull(oldObj)){
            throw new BaseException(ResponseCodeEnum.CHARGEPLAN_NON_EXIST.getMessage(),ResponseCodeEnum.CHARGEPLAN_NON_EXIST.getCode());
        }
        ChargePlan newObj=ChargePlan.builder()
                .id(chargePlanParam.getId())
                .heatYearId(chargePlanParam.getHeatYearId())
                .stopDate(chargePlanParam.getStopDate())
                .minPercent(chargePlanParam.getMinPercent())
                .build();
        return baseMapper.updateById(newObj)>0;
    }

    /**
     * 系统中是否存在指定供暖季的收费计划
     * @param heatYearId
     * @param id
     * @return
     */
    private boolean checkExist(Integer heatYearId, Integer id) {
        QueryWrapper<ChargePlan> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("heat_year_id",heatYearId);
        if (id!=null){
            queryWrapper.ne("id",id);
        }
        return this.list(queryWrapper).size()>0;
    }
}
